/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package caa.struts.report.action;

import caa.struts.report.form.ManageReportForm;
import com.googlecode.jthaipdf.jasperreports.engine.export.ThaiJRPdfExporter;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.view.JasperViewer;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import th.com.db.connect.PgConnect;
import th.com.entity.BranchEntity;
import th.com.entity.DepartmentEntity;
import th.com.entity.FacultyEntity;
import th.com.entity.ProgramEntity;
import th.com.entity.SubjectEntity;
import th.com.ku.CAAService;

/**
 *
 * @author omega
 */
public class ReportManageAction extends DispatchAction {

    public ActionForward doGetReport(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageReportForm addReportForm = (ManageReportForm) form;


        return mapping.findForward("getreport.success");
    }

    public ActionForward doGetReportApproval(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageReportForm addReportForm = (ManageReportForm) form;

        ArrayList<FacultyEntity> facList = CAAService.selectFacultyAll();
        addReportForm.setFacultyList(facList);

        return mapping.findForward("getreportapproval.success");
    }

    public ActionForward doAddListReportApproval(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageReportForm addReportForm = (ManageReportForm) form;

        if (addReportForm.getFacultyEntity() != null) {
            System.out.println("addReportForm.getFacultyEntity().getFacId() = " + addReportForm.getFacultyEntity().getFacId());
            List<DepartmentEntity> depList = CAAService.selectDepartmentAll(addReportForm.getFacultyEntity().getFacId());
            addReportForm.setDepartmentList(new ArrayList<DepartmentEntity>(depList));
        }
        if (addReportForm.getDepartmentEntity() != null) {
            System.out.println("addReportForm.getDepartmentEntity().getDepId() = " + addReportForm.getDepartmentEntity().getDepId());
            List<BranchEntity> braList = CAAService.selectBranchAll(addReportForm.getDepartmentEntity().getDepId());
            addReportForm.setBranchList(new ArrayList<BranchEntity>(braList));
        }
        if (addReportForm.getBranchEntity() != null) {
            System.out.println("addReportForm.getBranchEntity().getBraId() = " + addReportForm.getBranchEntity().getBraId());
            List<ProgramEntity> proList = CAAService.selectProgramAll(addReportForm.getBranchEntity().getBraId());
            addReportForm.setProgramList(new ArrayList<ProgramEntity>(proList));
        }
        return mapping.findForward("addlistreportapproval.success");
    }

    public ActionForward doCreatePaid(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageReportForm addReportForm = (ManageReportForm) form;
		
        byte[] byteout = this.reportWithResultSet(request.getSession().getServletContext().getRealPath("/jrxml/Approved_1_1.jrxml"), new HashMap<String, Object>(), null);
        //--- set program environment ---//
        if (byteout != null) {
//            OutputStream outputStream = response.getOutputStream();
            ServletOutputStream servletOutputStream = response.getOutputStream();
            response.setContentType("application/pdf");
            response.setContentLength(byteout.length);
            //response.setBufferSize(byteout.length);
            servletOutputStream.write(byteout, 0, byteout.length);
            //outputStream.flush();
            //outputStream.close();
            if (servletOutputStream != null) {
                servletOutputStream.flush();
                servletOutputStream.close();
            }
        }

//            ServletOutputStream servletOutputStream = response.getOutputStream();
////            InputStream reportStream = getServlet().getServletContext().getResourceAsStream("jrxmlName.jasper");
//            InputStream reportStream = new FileInputStream(request.getSession().getServletContext().getRealPath("/jrxml/report2.jasper"));
//            
//            HashMap<String, Object> Parameters = new HashMap<String, Object>();
////            Parameters.put("PrintDate", Common.GetCurrTime());
//            JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream, Parameters); 
//            response.setContentType("application/pdf");
//            servletOutputStream.flush();
//            servletOutputStream.close();
            

//        return mapping.findForward("getreportschedule.success");
          return null;
    }

    private byte[] reportWithResultSet(String fileReport, HashMap<String, Object> param, ResultSet rs) throws Exception {
        byte[] bs = null;
        try {
//            ThaiJRPdfExporter exporter = new ThaiJRPdfExporter();
//            exporter.set
            Connection conn = PgConnect.getConnection("caa_database", "postgres", "admin");
            JasperReport jasperReport =  JasperCompileManager.compileReport(fileReport);
//            param.put("PrintDate", "test");
            bs = JasperRunManager.runReportToPdf(jasperReport, param, conn);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, conn);
//            JasperRunManager.runReportToPdfFile(fileReport, "C://Users//Ketsaraporn//test123.pdf", param, conn);
            JasperExportManager.exportReportToPdfFile(jasperPrint, "C://Users//Ketsaraporn//test123.pdf");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return bs;
    }

    public ActionForward doGetReportSchedule(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageReportForm addReportForm = (ManageReportForm) form;
        addReportForm.setDepartmentList(new ArrayList<DepartmentEntity>());
        ArrayList<FacultyEntity> facList = CAAService.selectFacultyAll();
        addReportForm.setFacultyList(facList);
        System.out.println("dd = " + addReportForm.getFacultyEntity().getFacId());

        return mapping.findForward("getreportschedule.success");
    }
    
    public ActionForward doGetReportScheduleT(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageReportForm addReportForm = (ManageReportForm) form;
        addReportForm.setDepartmentList(new ArrayList<DepartmentEntity>());
        ArrayList<FacultyEntity> facList = CAAService.selectFacultyAll();
        addReportForm.setFacultyList(facList);
        System.out.println("dd = " + addReportForm.getFacultyEntity().getFacId());

        return mapping.findForward("getreportscheduleteacher.success");
    }

    public ActionForward doAddListReportSchedule(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ManageReportForm addReportForm = (ManageReportForm) form;

        if (addReportForm.getFacultyEntity() != null) {
            System.out.println("addReportForm.getFacultyEntity().getFacId() = " + addReportForm.getFacultyEntity().getFacId());
            List<DepartmentEntity> depList = CAAService.selectDepartmentAll(addReportForm.getFacultyEntity().getFacId());
            addReportForm.setDepartmentList(new ArrayList<DepartmentEntity>(depList));
        }

        return mapping.findForward("addlistreportschedule.success");
    }

}
